package edu.siu.math.egut.main;

import java.util.Scanner;

import edu.siu.math.egut.io.Algorithms;
import edu.siu.math.egut.util.Group;
import edu.siu.math.egut.util.Polynomial;
import edu.siu.math.egut.util.Sunipotent;

/**
 * 
 * Let w0 be an element of the Weyl group.  Fix a representation of w0 as a word in the 
 * simple reflections, and let w be the standard lifting of this word into N_G(T)(Z).
 * 
 *  Then for any root alpha we have 
 *  
 *  w x_alpha(r) w^{-1} = x_{w.\alpha}( +/- r).  
 *  
 *   The purpose of this program is to compute these signs.
 *   
 *   
 * 
 * @author Joseph Hundley
 *
 */
public class WeylSign {

    /**
     * @param args two args will be read.  the second should be the name of an exceptional group, i.e., 
     * one of {G2,F4,E6,E7,E8}, and the first should be parseable as a Weyl word in that group
     */
    public static void main(String[] args) {
	if( args.length ==0 ){
	    boolean quit = false;
	    Scanner scanner = new Scanner(System.in);
	    System.out.println("Enter the rank of the exceptional group you want to work in.");
	    int rank = Algorithms.getRank(scanner);
	    while(!quit){
		quit = interactiveWeylSign(scanner, rank);
	    }
	    System.exit(0);
	}
	else{
	if( args.length ==1 ){
	    System.out.println("This program has two modes.  One requires two arguments (a Weyl word and a root).\n" +
		    "The other requires no arguments." +
	    		"Exactly "+ args.length +" arguments detected.");
	    System.exit(1);
	}
	if(args.length > 2)
	    System.out.println("This program takes at most two arguments.\n"+ args.length +" arguments detected.\nExtra arguments" +
	    		" will be ignored.");
	
	
	int rank = args[1].length();
	
	Group g = Group.getGroup(rank);
	if( g == null){
	    System.out.println("The length of the second argument --\""+args[1]+"\"-- \nis not the rank of an exceptional group.\n");
	    System.exit(1);
	}
	
	int rootInteger =1;
	try{
	    rootInteger = Integer.parseInt(args[1]);
	}
	catch(NumberFormatException e){
	    System.out.print("Second argument  --\""+args[1]+"\"-- is not an integer.");
	    e.printStackTrace();
	    System.exit(1);	    
	}
	int[] a = Algorithms.rootFromInt(rootInteger, rank);
	
	

	
	Sunipotent s= Sunipotent.create(a, Polynomial.create("r"));
	System.out.print("w["+args[0]+"]."+s.toString()+".w["+args[0]+"]^{-1}=\n");
	System.out.println(s.imageUnder(args[0]));
	
	}

    }

    /*
     * This method is for the case when the reader wants to do several Weyl element root combinations.
     */
    private static boolean interactiveWeylSign(Scanner scanner, int rank) {
	System.out.println("Input a weyl element.");
	String w = scanner.nextLine();
	System.out.println(w);
	if( w.trim().matches("0"))
	    return true;
	w=edu.siu.math.egut.io.Algorithms.getWeylElement(
		edu.siu.math.egut.io.Algorithms.removeWhiteSpaceCommasAndBrackets(w), rank, scanner);
	System.out.println("Input a root.");
	int[] a=edu.siu.math.egut.io.Algorithms.getRoot(scanner, Group.getGroup(rank));
	Sunipotent s= Sunipotent.create(a, Polynomial.create("r"));
	System.out.print("w["+w+"]."+s.toString()+".w["+w+"]^{-1}=\n");
	System.out.println(s.imageUnder(w));

	return false;
    }

}
